package cn.caixiong.org.controller;


import cn.caixiong.basic.util.AjaxResult;
import cn.caixiong.basic.util.PageList;
import cn.caixiong.org.domain.Syst;
import cn.caixiong.org.query.SystQuery;
import cn.caixiong.org.service.ISystService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController//交给Spring管理
@RequestMapping("/syst")
@Api(tags = "部门管理接口", description = "部门管理接口描述")
public class SystController {
    //注入业务层
    @Autowired
    private ISystService systService;

    //分页查询
    @PostMapping("/findPage")
    @ApiOperation(value = "分页查询所有部门接口", notes = "分页查询部门列表")
    public PageList<Syst> findPage(@RequestBody SystQuery query) {
        //PageList<syst> page = systService.findPage(query);
        return systService.findPage(query);
    }

    //查询所有
    @GetMapping("/findAll")
    @ApiOperation(value = "查询所有部门接口", notes = "上级部门列表")
    public AjaxResult findAll() {
        return AjaxResult.createSuccess(systService.findAll());
    }





    //新增或修改
    @PostMapping
    @ApiOperation(value = "新增修改部门接口", notes = "没有id就是新增")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "department", value = "部门对象", required = true)})
    public AjaxResult saveOrUpdate(@RequestBody Syst department) {
        try {
            systService.saveOrUpdate(department);
            return AjaxResult.createSuccess(null);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError("操作异常，请联系管理员");
        }

    }

    @DeleteMapping("{id}")
    @ApiOperation(value = "删除部门接口", notes = "根据删除id对象")
    public AjaxResult delete(@PathVariable("id") Long Id) {
        try {
            systService.delete(Id);
            return AjaxResult.createSuccess(null);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError("操作异常，请联系管理员");
        }

    }

    @PatchMapping
    @ApiOperation(value = "批量删除部门接口", notes = "批量删除id对象")
    public AjaxResult batchDelete(@RequestParam("ids") Long[] Ids) {
        try {
            systService.batchDelete(Ids);
            return AjaxResult.createSuccess(null);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.createError("操作异常，请联系管理员");
        }

    }


}
